package com.google.project.client.Director;

import java.util.ArrayList;
import java.util.List;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.project.client.Service.LoginService;
import com.google.project.client.Service.LoginServiceAsync;
import com.google.project.client.Service.MessageService;
import com.google.project.client.Service.MessageServiceAsync;
import com.google.project.client.Service.ProjectReportService;
import com.google.project.client.Service.ProjectReportServiceAsync;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.shared.MessageDTO;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectReportDTO;
import com.google.project.shared.UsersDTO;

public class DRTHomePage extends Composite {

private static final Binder binder = GWT.create(Binder.class);
	
	@UiField Hyperlink hypMessNew;
	@UiField Hyperlink hypProjectNew;
	@UiField Hyperlink hypProjectFinish;
	@UiField Hyperlink hypProjectStart;
	@UiField Label lblTitle;
	@UiField Label lblMess;
	@UiField Label lblProjInfo;
	@UiField Label lblProjectReport;
	@UiField Hyperlink hypProjectReport;
	@UiField Hyperlink humanReqMessage;

	interface Binder extends UiBinder<Widget, DRTHomePage> {
	}

	private final LoginServiceAsync loginService = GWT.create(LoginService.class);
	private final MessageServiceAsync messageService = GWT.create(MessageService.class);
	private final ProjectServiceAsync projectService = GWT.create(ProjectService.class);
	private final ProjectReportServiceAsync projectReportSv = GWT.create(ProjectReportService.class);
	List<MessageDTO> listMessage = new ArrayList<MessageDTO>();
	MessageDTO messDTO = new MessageDTO();
	UsersDTO userDTO = new UsersDTO();
	
	public DRTHomePage() {
		initWidget(binder.createAndBindUi(this));
		getUserInfo();
		getAllMess();
		getHumanReqMessage(0,1);
		getNumOfProjByStatus(0);
		getNumOfProjByStatus(1);
		getNumOfProjByStatus(2);
	}
	
	// get info user login from session
	public void getUserInfo() {
		loginService.getCurrentUserDTO(new AsyncCallback<UsersDTO>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(UsersDTO result) {
				userDTO = result;
			}
		});
	}
	// getAll dailyReport where drId + statusOfReport = 1-chưa đọc
	public void getReportByDR(String _userID) {
		projectReportSv.getAllProjectReport(
				new AsyncCallback<List<ProjectReportDTO>>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Có lỗi xảy ra!");
					}

					@Override
					public void onSuccess(List<ProjectReportDTO> result) {
						List<ProjectReportDTO> list=new ArrayList<ProjectReportDTO>();
						for (ProjectReportDTO projectReportDTO : result) {
							if(projectReportDTO.getStatus()==1){ //báo cáo mới
								list.add(projectReportDTO);
							}
						}
						
						if(list.size() == 0){
							hypProjectReport.setText("Báo cáo chưa đọc: 0 (Báo cáo)");
						}else{
							hypProjectReport.setText("Báo cáo chưa đọc: " + list.size() + " (Báo cáo)");
						}
					}
				});
	}
	// get all Message
	public void getAllMess() {
		messageService.getMessageList(new AsyncCallback<List<MessageDTO>>() {

			@Override
			public void onSuccess(List<MessageDTO> result) {
				for (MessageDTO messageDTO : result) {
					listMessage.add(messageDTO);
				}
				loadMess();
			}

			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}

	// get Message where (userId=receiveId and status=1) or (userId=senderId and
	// status=2) & parentID=0
	private void loadMess() {
		Boolean condition = true;
		List<MessageDTO> temple = new ArrayList<MessageDTO>();

		for (MessageDTO mess : listMessage) {
			condition = condition && (mess.getTypeId() == 1)
					&& (mess.getParentId().equalsIgnoreCase("0") && (((mess.getStatus() == 1)
							&& mess.getReceiverId().equals(userDTO.getUserId()) || ((mess.getStatus() == 2) && mess.getSenderId().equals(userDTO.getUserId())))));
			if (condition) {
				temple.add(mess);
			}
			condition = true;
		}
		 getReportByDR(userDTO.getUserId());
		//đếm tin nhắn mới
		if (temple != null) {
			hypMessNew.setText("Tin nhắn chưa đọc: " + temple.size() + " (Tin nhắn)");
		}else{
			hypMessNew.setText("Tin nhắn chưa đọc: 0 (Tin nhắn)");
		}
	}
	
	/***
	 * Lấy danh sách dự án theo userID và trạng thái project
	 * 0-mới, 1-đang tiến hành
	 */
	public static int num = 0;
	public void getNumOfProjByStatus(final int status) {
	
		projectService.getProjectList(new AsyncCallback<List<ProjectDTO>>() {

			@Override
			public void onSuccess(List<ProjectDTO> result) {
				List<ProjectDTO> list = new ArrayList<ProjectDTO>();
				for (ProjectDTO projectDTO : result) {
					if(projectDTO.getStatus() == status){
						list.add(projectDTO);
					}
				}
				num = list.size();
				if(status == 0){
					hypProjectNew.setText("Dự án mới: "+ num + " (Dự án)");
				}else if(status == 1){
					hypProjectStart.setText("Dự án đang triển khai: " + num + " (Dự án)");
				}
				else if(status == 2){
					hypProjectFinish.setText("Dự án đã kết thúc: "+ num + " (Dự án)");
				}
				lblTitle.setText("THÔNG TIN TỔNG HỢP");
				lblMess.setText("Các vấn đề cần giải quyết:");
				lblProjectReport.setText("Báo cáo dự án: ");
				lblProjInfo.setText("Thông tin dự án: ");
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Có lỗi trong quá trình Tải thông tin!");
			}
		});
	}
	
	//Lay thong tin Yeu cau nhan su cua cac du an moi - co isReq = 1
	public void getHumanReqMessage(final int _status,final int _isReq) {
		projectService.getProjectList(new AsyncCallback<List<ProjectDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<ProjectDTO> result) {
				List<ProjectDTO> list = new ArrayList<ProjectDTO>();
				int size = 0;
				for (ProjectDTO projectDTO : result) {
					if(projectDTO.getStatus() == _status && projectDTO.getIsReq()==_isReq){
						list.add(projectDTO);
						size ++;
					}
				}
				humanReqMessage.setText("Yêu cầu nhân sự: " + size + " (Yêu cầu)");
			}
		});
	}
	
	//tin nhắn mới
	@UiHandler("hypMessNew")
	void onHypMessNewClick(ClickEvent event) {
		DRTEntrypoint.get().DRTMessList();
	}

	@UiHandler("hypProjectNew")
	void onHypProjectNewClick(ClickEvent event) {
		int status = 0; // dự án mới
		DRTEntrypoint.get().DRTProjectList(status);
	}

	@UiHandler("hypProjectStart")
	void onHypProjectStartClick(ClickEvent event) {
		int status = 1; //dự án đang triển khai
		DRTEntrypoint.get().DRTProjectList(status);
	}
	
	@UiHandler("hypProjectFinish")
	void onHypProjectFinishClick(ClickEvent event) {
		int status = 2; //dự án đã kết thúc
		DRTEntrypoint.get().DRTProjectList(status);
	}
	

	@UiHandler("hypProjectReport")
	void onHypProjectReportClick(ClickEvent event) {
		DRTEntrypoint.get().DRTProjectReportList();
	}
	
	@UiHandler("humanReqMessage")
	void onHumanReqMessageClick(ClickEvent event) {
		DRTEntrypoint.get().DRTHumanReq(1);	//Lay ra nhung yeu cau nhan su chua duoc phe duyet
	}
	
}
